package com.entities.user.detail;


import java.util.Iterator;
import java.util.List;

import com.core.protocols.ICustomDatasourceDelegate;
import com.core.protocols.IDescriptionViewDelegate;
import com.core.protocols.IDetailTableViewDelegate;
import com.core.protocols.IServiceLayerDelegate;
import com.core.protocols.ITabSheetDatasource;
import com.core.servicelayer.HibernateServiceLayer;
import com.core.ui.controller.base.HibernateDetailController;
import com.core.ui.customcell.TabSheetItemCell;
import com.core.utils.KeyValueDictionary;
import com.core.utils.LocalizableString;
import com.entities.user.servicelayer.UserListLayer;
import com.entities.userrole.servicelayer.UserRoleServiceLayer;
import com.model.KeyValue;
import com.model.user.User;
import com.model.user.UserRole;


public class UserDetailController
        extends HibernateDetailController
        implements IDetailTableViewDelegate, IDescriptionViewDelegate, IServiceLayerDelegate, ITabSheetDatasource {


	private String               userId;
	private UserListLayer        userListLayer           = null;
	private User                 userItem;

	private UserRoleServiceLayer userRoleServiceLayer    = null;

	private final int            userRoleRequestUserInfo = 301;

	private KeyValueDictionary   userRoleDictionary      = null;



	public UserDetailController() {

		userListLayer = new UserListLayer();

	}



	public String getUserId () {

		return userId;
	}



	public void setUserId ( String userId ) {

		this.userId = userId;
	}



	/**
	 * ICustomDatasource
	 * 
	 * 
	 */


	@Override
	public void reloadData () {


		this.userRolePerformFetchItems();

		Integer result = null;
		try {
			result = Integer.parseInt(this.userId);
		} catch ( NumberFormatException e ) {
			result = null;
		}


		userItem = (User) userListLayer.objectByUNID(result, null, this);

	}



	@Override
	public void setCustomDatasourceDelegate ( ICustomDatasourceDelegate delegate ) {

		// TODO Auto-generated method stub

	}



	/**
     *
     *
     *
     *
     */


	@Override
	public int tabSheetCellsCount () {

		// TODO Auto-generated method stub
		return 0;
	}



	@Override
	public TabSheetItemCell tabSheetCellAtIndex ( int index ) {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String tabsPanelStyleClassName () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public String descriptionData () {

		// TODO Auto-generated method stub
		return null;
	}



	@Override
	public int detailTableViewItemsCount ( String userInfo ) {

		int result = 0;
		String userInfoUC = userInfo.toUpperCase();
		if ( userInfoUC.equals("BASE") ) {
			result = 4;
		}
		return result;
	}



	@Override
	public String detailTableViewItemAtIndex ( String userInfo,
	                                           int index ) {

		String result = null;

		if ( userInfo.toUpperCase().equals("BASE") ) {
			result = this.userBaseItemAtIndex(index);
		}
		return result;
	}



	@Override
	public <T> void responseResponseItemsList ( final List <T> items,
	                                            final Object request,
	                                            final Exception exception ) {

		if ( request == null ) {
			return;
		}

		HibernateServiceLayer serviceLayer = (HibernateServiceLayer) request;
		Integer userInfo = (Integer) serviceLayer.getUserInfo();
		if ( userInfo == null ) {
			return;
		}


		switch ( userInfo ) {
			case userRoleRequestUserInfo: {
				this.userRoleResponseItems(items);
				break;
			}
			default: {
				break;
			}
		}
	}



	private String userBaseItemAtIndex ( int index ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		String result = null;

		String style = null;
		String propertyValue = null;
		String data = null;

		boolean readOnly = this.isReadOnly();

		switch ( index ) {
			case 0: {
				style = "-single";
				if ( userItem != null ) {
					propertyValue = userItem.getName();
				}
				String caption = localizableString.localizable("entity.User.field.name");
				data = this.inputControl("name", caption, propertyValue, readOnly);
				break;
			}
			case 1: {
				style = "-single";
				if ( userItem != null ) {
					propertyValue = userItem.getDescription();
				}
				String caption = localizableString.localizable("entity.User.field.description");
				data = this.textAreaControl("description", caption, propertyValue, readOnly);
				break;
			}
			case 2: {
				style = "-single";
				data = this.userRoleCell(userItem, readOnly);
				break;
			}
			case 3: {
				style = "-single";
				if ( userItem != null ) {
					propertyValue = userItem.getMailAddress();
				}
				String caption = localizableString.localizable("entity.User.field.email");
				data = this.inputControl("mailAddress", caption, propertyValue, readOnly);

			}
			default: {
				break;
			}
		}
		result = String.format("<li class=\"property-item%s\"><div class=\"wrap\">%s</li>", style, data);
		return result;
	}



	private String userRoleCell ( User user,
	                              boolean readOnly ) {

		LocalizableString localizableString = LocalizableString.sharedInstance();

		String result = null;
		KeyValue propertyValue = null;
		if ( user != null ) {
			UserRole userRole = user.getUserCategory().getUserRole();
			if ( userRole != null ) {
				String id = String.format("%d", userRole.getId());
				String name = userRole.getName();
				propertyValue = new KeyValue(id, name);
			}
		}
		String caption = localizableString.localizable("entity.User.field.role");
		result = this.selectControl("userRole", caption, propertyValue, userRoleDictionary, readOnly);
		return result;

	}



	private void userRolePerformFetchItems () {

		userRoleServiceLayer = new UserRoleServiceLayer();
		userRoleServiceLayer.setUserInfo(new Integer(userRoleRequestUserInfo));
		userRoleServiceLayer.setLayerDelegate(this);
		userRoleServiceLayer.performRequestWithFilter(null, null);

	}



	private <T> void userRoleResponseItems ( List <T> items ) {

		userRoleDictionary = new KeyValueDictionary();
		userRoleDictionary.put(null, "-");
		Iterator <T> iterator = items.iterator();
		while ( iterator.hasNext() ) {
			UserRole role = (UserRole) iterator.next();
			userRoleDictionary.put(String.format("%d", role.getId()), role.getName());
		}

	}



}
